package com.service.impl;

import com.dao.FilmMapper;
import com.entity.Film;
import com.service.FilmService;
import com.vo.FilmDetailVO;
import com.vo.FilmVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FilmServiceImpl implements FilmService {
    @Autowired
    private FilmMapper filmDao;
    @Override
    public List<FilmDetailVO> selectAll() {
        List<Film> films = filmDao.selectAll();
        List<FilmDetailVO> returns = new ArrayList<>();
        for(Film f : films){
            FilmDetailVO fDVO = new FilmDetailVO();
            fDVO.setF_id(f.getF_id());
            fDVO.setF_name(f.getF_name());
            fDVO.setF_director(f.getF_director());
            fDVO.setF_country(f.getF_country());
            fDVO.setF_length(f.getF_length());
            fDVO.setF_synopsis(f.getF_synopsis());
            fDVO.setF_time(f.getF_time());
            fDVO.setF_type(f.getF_type());
            fDVO.setImg_path(f.getImg_path());
            returns.add(fDVO);
        }
        return returns;
    }

    @Override
    public FilmDetailVO filmDetail(String filmId) {
        Film f = filmDao.filmDetail(filmId);
        FilmDetailVO fDVO = new FilmDetailVO();
        fDVO.setF_id(f.getF_id());
        fDVO.setF_name(f.getF_name());
        fDVO.setF_director(f.getF_director());
        fDVO.setF_country(f.getF_country());
        fDVO.setF_length(f.getF_length());
        fDVO.setF_synopsis(f.getF_synopsis());
        fDVO.setF_time(f.getF_time());
        fDVO.setF_type(f.getF_type());
        fDVO.setImg_path(f.getImg_path());
        return fDVO;
    }

    @Override
    public boolean filmById(String filmId) {
        return filmDao.filmDetail(filmId) == null ? true : false;
    }

    @Override
    public int insert(FilmVO filmVO) {
        Film film = new Film();
        film.setF_id(filmVO.getFilmId());
        film.setF_name(filmVO.getName());
        film.setF_director(filmVO.getDirector());
        film.setF_players(filmVO.getPlayer());
        film.setF_country(filmVO.getCountry());
        film.setF_length(filmVO.getLength());
        film.setF_synopsis(filmVO.getSynopsis());

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            film.setF_time(format.parse(filmVO.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        film.setF_type(filmVO.getType());
        film.setImg_path(filmVO.getImgPath());
        System.out.println(film);
        return filmDao.insert(film);
    }

    @Override
    public List<FilmVO> filmAll(int page,int limit) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd ");
        Map<String,Integer> map = new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        List<Film> films = filmDao.selectPage(map);
        List<FilmVO> filmVOS = new ArrayList<>();
        for (Film film : films) {
            FilmVO filmVO = new FilmVO();
            filmVO.setFilmId(film.getF_id());
            filmVO.setName(film.getF_name());

            filmVO.setImgPath(film.getImg_path());

            filmVO.setDirector(film.getF_director());
            filmVO.setPlayer(film.getF_players());
            filmVO.setType(film.getF_type());
            filmVO.setCountry(film.getF_country());
            filmVO.setLength(film.getF_length());
            filmVO.setSynopsis(film.getF_synopsis());


            StringBuffer dateStr = new StringBuffer(format.format(film.getF_time()));
            dateStr.replace(4,5,"年");
            dateStr.replace(7,8,"月");
            dateStr.replace(10,11,"日 ");
            filmVO.setTime(dateStr.toString());

            filmVO.setSetup("<a href='' target='content' class='layui-btn layui-btn-sm layui-btn-danger'>修改</a>");

            filmVOS.add(filmVO);
        }

        return filmVOS;
    }

    @Override
    public int count() {
        return filmDao.selectAll().size();
    }
}
